﻿using project.B12.IRepository;
using project.B12.IRepository.Base;
using project.B12.Model.Models;
using project.B12.Repository.Base;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace project.B12.Repository.Repository
{
    public class HisTableRepository : SugarRepository<Table_His>, IHisRepository
    {

        private IUnitOfWork UOW;
        public HisTableRepository(IUnitOfWork pUOW) : base(pUOW)
        {
            UOW = pUOW;
        }

        /// <summary>
        /// 新增历史数据
        /// </summary>
        /// <param name="pNewProject"></param>
        public async Task<bool> AddNewHis(Table_His pNew)
        {
            try
            {
                UOW.BeginTran();
                await Add(pNew);
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
            return true;
        }

        public bool UpdateHis(Table_His pNew)
        {
            try
            {
                UOW.BeginTran();
                Update(pNew);
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
            return true;
        }

        public List<Table_His> QueryByTime(DateTime? startTime, DateTime? endTime)
        {
            List<Table_His> result=new List<Table_His>();
            try
            {
                UOW.BeginTran();
                if (startTime != null && endTime != null)
                    result = Query(x => x.LoadTime >= startTime && x.LoadTime <= endTime).Result;
                else if (startTime != null)
                    result = Query(x => x.LoadTime >= startTime).Result;
                else if (endTime != null)
                    result = Query(x => x.LoadTime <= endTime).Result;
                else
                    result = Query().Result;
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
            return result;
        }


        /// <summary>
        /// 根据时间段查询结果
        /// </summary>
        /// <returns></returns>
        public List<Table_His> QueryHisByTimeSlot(DateTime start, DateTime end)
        {
            List<Table_His> result;
            try
            {
                UOW.BeginTran();
                result = Query(x => x.LoadTime > start && x.LoadTime < end).Result;
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
            return result;
        }
        ///// <summary>
        ///// 根据ID查询结果
        ///// </summary>
        ///// <returns></returns>
        //public List<Table_His> QueryHisByID(int ID)
        //{
        //    List<Table_His> result;
        //    try
        //    {
        //        UOW.BeginTran();
        //        result = Query(x => x.ID == ID).Result;
        //        UOW.CommitTran();
        //    }
        //    catch (Exception ex)
        //    {
        //        UOW.RollbackTran();
        //        throw ex;
        //    }
        //    return result;
        //}

        /// <summary>
        /// 根据药敏板名称查询结果
        /// </summary>
        /// <returns></returns>
        public List<Table_His> QueryHisByPlateName(string pname,string sampleId)
        {
            List<Table_His> result;
            try
            {
                UOW.BeginTran();
                result = Query(x => x.PlateName == pname&&x.BacterialID==sampleId).Result;
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
            return result;
        }

        ///// <summary>
        ///// 查询所有结果
        ///// </summary>
        ///// <returns></returns>
        //public List<HisTable> QueryAllHis()
        //{
        //    List<HisTable> result;
        //    try
        //    {
        //        UOW.BeginTran();
        //        result = Query().Result;
        //        UOW.CommitTran();
        //    }
        //    catch (Exception ex)
        //    {
        //        UOW.RollbackTran();
        //        throw ex;
        //    }
        //    return result;
        //}

    }
}
